package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.mapper.PermissionMapper;
import org.javaweb.cms_web.model.Enum.PermissionTypes;
import org.javaweb.cms_web.model.Feedback;
import org.javaweb.cms_web.model.User;
import org.javaweb.cms_web.model.UserPermission;
import org.javaweb.cms_web.service.FeedbackService;
import org.javaweb.cms_web.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;
    @Resource
    private HostHolder hostHolder;
    @Resource
    private PermissionMapper permissionMapper;

    // 显示所有反馈，并支持搜索和筛选
    @GetMapping("/admin/FeedbacksManagement")
    public String showFeedbacksList(@RequestParam(value = "searchKeyword", required = false) String searchKeyword,
                                    @RequestParam(value = "statusFilter", required = false) String statusFilter,
                                    @RequestParam(value = "timeFilter", required = false) String timeFilter,
                                    Model model) {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.ONLINE_COMMUNICATION.value)) {
            return "nopermission";
        }
        List<Feedback> feedbacks = feedbackService.getFeedbacksWithFilters(searchKeyword, statusFilter, timeFilter);
        model.addAttribute("feedbacks", feedbacks);
        model.addAttribute("searchKeyword", searchKeyword);
        model.addAttribute("statusFilter", statusFilter);
        model.addAttribute("timeFilter", timeFilter);
        return "feedback-management";  // 返回反馈管理页面
    }

    // 查看单条反馈详情
    @GetMapping("/admin/Feedbacks/{id}")
    public String viewFeedbackDetail(@PathVariable("id") Integer feedbackId, Model model) {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.ONLINE_COMMUNICATION.value)) {
            return "nopermission";
        }
        Feedback feedback = feedbackService.getFeedbackById(feedbackId);
        model.addAttribute("feedback", feedback);
        return "feedback-detail";  // 返回到反馈详情页面
    }

    // 回复反馈并更新状态
    @PostMapping("/admin/Feedbacks/reply")
    public String replyToFeedback(@RequestParam("feedbackId") Integer feedbackId,
                                  @RequestParam("replyContent") String replyContent,
                                  Model model) {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.ONLINE_COMMUNICATION.value)) {
            return "nopermission";
        }
        boolean success = feedbackService.replyToFeedback(feedbackId, replyContent);
        if (success) {
            model.addAttribute("message", "回复成功");
        } else {
            model.addAttribute("message", "回复失败");
        }
        return "redirect:/admin/FeedbacksManagement";  // 重定向到反馈管理页面
    }

    // 关闭反馈
    @GetMapping("/admin/feedbacks/close/{id}")
    public String closeFeedback(@PathVariable("id") Integer feedbackId, Model model) {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.ONLINE_COMMUNICATION.value)) {
            return "nopermission";
        }
        boolean success = feedbackService.closeFeedback(feedbackId);
        if (success) {
            model.addAttribute("message", "反馈已关闭");
        } else {
            model.addAttribute("message", "关闭反馈失败");
        }
        return "redirect:/admin/FeedbacksManagement";  // 重定向回反馈管理页面
    }
}

